package com.webfinal.backsystem.service.impl;

import com.webfinal.backsystem.dao.UserMapper;
import com.webfinal.backsystem.entity.User;
import com.webfinal.backsystem.request.user.LoginRequest;
import com.webfinal.backsystem.request.user.RegisterRequest;
import com.webfinal.backsystem.request.user.UpdateInfoRequest;
import com.webfinal.backsystem.response.user.LoginResponse;
import com.webfinal.backsystem.response.user.RegisterResponse;
import com.webfinal.backsystem.response.user.UpdateInfoResponse;
import com.webfinal.backsystem.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    private final static Logger LOG= LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;
    @Override
    public LoginResponse login(LoginRequest request) {
        LoginResponse response=new LoginResponse();
        try {
            User user=userMapper.selectByAccount(request.getAccount());
            if (user==null){
                response.setResultCode("1");
                response.setResultMsg("账号不存在");
                return response;
            }
            if (!user.getPassword().equals(request.getPassword())) {
                response.setResultMsg("密码错误");
                response.setResultCode("1");
                return response;
            }
            if (user.getAuthority()<request.getFlag()) {
                response.setResultCode("1");
                response.setResultMsg("账号无访问权限");
                return response;
            }
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setResult(user);
        } catch (Exception e) {
            LOG.error("UserServiceImpl.login异常>>>>>>>",e);
            response.setResultMsg("登录服务异常");
            response.setResultCode("1");
            return response;
        }
        return response;
    }

    @Override
    public RegisterResponse register(RegisterRequest request) {
        RegisterResponse response=new RegisterResponse();
        try {
            User user=new User();
            user.setUserId(0);
            user.setAccount(request.getAccount());
            user.setPassword(request.getPassword());
            // 默认用户名为账号
            user.setUsername(request.getAccount());
            user.setAuthority(0);
            if (userMapper.insert(user)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
            }
        }catch (Exception e) {
            LOG.error("用户表插入数据失败");
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public UpdateInfoResponse updateInfo(UpdateInfoRequest request) {
        UpdateInfoResponse response=new UpdateInfoResponse();
        User user=new User();
        User tmp=userMapper.selectByAccount(request.getAccount());
        if (tmp==null){
            response.setResultCode("1");
            response.setResultMsg("未查到账户信息");
            return response;
        }
        // 设置主键
        user.setUserId(tmp.getUserId());
        if (request.getNewAccount()!=null && !request.getNewAccount().equals("")){
            user.setAccount(request.getNewAccount());
            tmp.setAccount(request.getNewAccount());
        }
        if (request.getPassword()!=null && !request.getPassword().equals("")){
            if (request.getPassword().equals(tmp.getPassword())){
                user.setPassword(request.getNewPassword());
                tmp.setPassword(request.getNewPassword());
            }
        }
        if (request.getSex()!=null){
            user.setSex(request.getSex());
            tmp.setSex(request.getSex());
        }
        if (request.getNewUsername()!=null && !request.getNewUsername().equals("")){
            user.setUsername(request.getNewUsername());
            tmp.setUsername(request.getNewUsername());
        }
        if (request.getAuthority()!=null){
            user.setAuthority(request.getAuthority());
            tmp.setAuthority(request.getAuthority());
        }
        if (request.getBirthday()!=null && !request.getBirthday().equals("")){
            user.setBirthday(request.getBirthday());
            tmp.setBirthday(request.getBirthday());
        }
        try {
            if (userMapper.updateByPrimaryKey(user)==1){
                response.setResultCode("0");
                response.setResultMsg("success");
                response.setResult(tmp);
            }
        }catch (Exception e){
            LOG.error("更新表异常",e);
            response.setResultCode("1");
            response.setResultMsg("服务异常");
            return response;
        }
        return response;
    }
}
